package longrise.phone.com.baidulib.baidu;

import android.content.Context;
import android.location.LocationManager;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**

 * Created by luoyang on 2017-07-26.

 */

public class LocationHelper implements BDLocationListener {

    private LocationClient locationClient;
    private LocationListener mListener;
    private LocationClientOption mOption;
    private Context mContext;



    public LocationHelper(Context context, LocationListener listener) {
        if(null == context)
        {
            return;
        }

        this.mListener = listener;
        this.mContext = context;
        initLocationClientOption();
        //第一步实例化定位核心类
        locationClient = new LocationClient(context.getApplicationContext(),
                mOption);
        locationClient.registerLocationListener(this);
    }

    private void initLocationClientOption() {
        mOption = new LocationClientOption();
        mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        mOption.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，如果配合百度地图使用，建议设置为bd09ll;
//        mOption.setScanSpan(locationSpaceTime * 1000);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        mOption.setScanSpan(0);
        mOption.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        mOption.setIsNeedLocationDescribe(false);//可选，设置是否需要地址描述
        mOption.setOpenGps(true);
    }

    public void start()
    {
        if(!checkGPS()) {
            mListener.showGpsDialog();
            return ;
        }
        if(locationClient != null) {
            locationClient.start();
        }
    }



    //一般会在Activity的OnDestroy方法调用
    public void stop()
    {
        if (locationClient != null && locationClient.isStarted())
        {
            locationClient.stop();
        }
    }

    public void destroy()
    {
        if (locationClient != null)
        {
            locationClient.unRegisterLocationListener(this);
            locationClient.stop();
            locationClient = null;
        }
    }

    //检测GPS功能是否开启
    private boolean checkGPS() {
        LocationManager manager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return false;
        }
        return true;
    }



    @Override
    public void onReceiveLocation(BDLocation location) {
        boolean success = false;
        if (location.getLocType() == BDLocation.TypeGpsLocation)
        {
            //gps定位成功
            success = true;
        }
        else if (location.getLocType() == BDLocation.TypeNetWorkLocation)
        {
            //网络定位成功
            success = true;
        }
        else if (location.getLocType() == BDLocation.TypeOffLineLocation)
        {
            //离线定位成功，离线定位结果也是有效的
            success = true;
        }
        else if (location.getLocType() == BDLocation.TypeServerError)
        {
            //服务端网络定位失败，可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com，会有人追查原因
        }
        else if (location.getLocType() == BDLocation.TypeNetWorkException)
        {
            //网络不同导致定位失败，请检查网络是否通畅
        }
        else if (location.getLocType() == BDLocation.TypeCriteriaException)
        {
            //无法获取有效定位依据导致定位失败，一般是由于手机的原因，处于飞行模式下一般会造成这种结果，可以试着重启手机
        }

        if(mListener == null) {
            return ;
        }

        if(success) {
            Log.i("百度定位成功" , "经度："+ location.getLatitude() + "  " +
                    "纬度：" + location.getLongitude() + "  时间:" + location.getTime());
            mListener.locationSuccess(location);
        } else {
            mListener.locationFail();
        }
        stop();
    }

    public interface LocationListener {
        void locationSuccess(BDLocation location);
        void locationFail();
        void showGpsDialog();
    }
}
